package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    public static String app_id = "2021000118642238";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7WXkLqGk9jMNIdonxNtZlQkJ2a++kOOZlYTLJ67ZYeYQWk+v+pIKQIMlnvFG5nJs6xpS+kS5LxWTZ1Csq68nN680TYLPxUUlQ+JDF+Msft1eDW2ZfFGMbjbg46JWYqFcKmAVcaWO+gv+rIboOFMDjipUcLB+56UQSNUJEHzpklJefrfofNH4acEtp7fk79i1lEQkUs16mSbkU8U1g6BLNAAtPLDyqwCnoNKqfs+3qQw61keRjhiH32B+K743UCMeGEMBlEG9emUaon3EP9StmTqVG1OuUQV/Xwmm2801IBEZpbnHaH8ad8Q/5dG268hv9qM/G08txyoPpFdAxAxdzAgMBAAECggEAVBIteGbFeyJMi5ruAblq8huOuZdr/VSvRvUcFnJY7FDzTjJo52cpU8Xyu0NNpS46lmCAZRe/H69ft0Q3btZKQ8a5yyVM+lBQxy4TwVMIy525fq+oO6hpZPbuoVL1toBsNsvLNBHy+Y3Nbxz+Yc08VZ9dlDvUrNeZfVY6+1jMGk+zMfTTqz9npsnx8elCJhVLpR1D/vHwv3Fb2Rbbbn1NPfrlkZTlCK9mHRWgW15uKrL3ghd82iZCWDMUMq4e/tloFERFc330FtZgQY7iecMzHFIM31/MCsdwPuKQlFqB4NW4ssienu6zehrsBcG0xP+iNaHEIlcfEAk0NuLIdRIzqQKBgQDggXgw/80ld8MSzvJiNRpAyLsztzWsukA/jpqsgIMbq50mIpyhCanxuliFTIYA1UChrTsbiI4jGxhn67RISH/elu8tZey/p7yn1our94/PwFViPg7TS2T3wBlwAeQ8+xx25Gmt3AZkwR2+2eym8Z5IxBgJEfUlnwjah5BEbcHx3wKBgQDVoaKgD7eLau0A7j4aWnlEJscJ5Z4UYP54IwTcCkjID55aYVoGP+BWn9fVREmM76xggX4hf+pVWXEVS/DowrqCtSL4LL/GsCnrGfyLjzynxrGVrs2+JmCCvmcKhFiOcpnRy74UNhyiYmf3zA/uxS4xPkikMx9HCzjqXJPNLAdU7QKBgQCe3D5WPcvGJXaNyo5M29KV4PRGZe+/J5Pq5VehlNYxMEBlCbzXXuU4PWoB+OIhmMTBIrlehEQ/px6f+nN5aoPA2ACqmSLrgx/Fi9UNfIHwueIxQERxUzpA7VeSoGQGmpHLt1joQ3bquJP5xm7kurgMOWXglSpo0p4LND3rjdbc9QKBgQCSJUt9uILWK8YLVvDBUpeXGfgcOytTwa8UPNC8P4Te6GBqcCMIF3a/jAY5bcfrVKNWOgAxprKGxPNpoFQkx+KUPJq5sNNZd3aV0WdFii3ykSk3vygdJ1/q0ZroRPXYLWoxbfKRHRulszBxAvb7X4VkRrmnKxoAnhrHqaGrQK2bWQKBgQDZg3/Q1VAztJNSGTtcs96AWohvgTVPpZyMcMeyqA5F0a1ZkAN7mxG2AXeaDRBzTExqc59Hblri1ke0fTFMNoGcmq6R9d34z4rlVrbrXbeHBBpwm1O9zLwX+cUNFWnMzTK8tf88GO4GzoT9hzR/D6D/lhRW52SetBjKgqu1DApZTQ==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr1UIWz1mZ4gHHc0wpgNiYJHnD2w9zyp19zLDytoHq5IzI6Qjg0jtLkHpwg2S3QKRQps3RU1PS8XuNR6yb07Zb1Op8cjv/o2iw932hHbOpkQbjyAGt+11Vbhrxj7eeqdbiZsnItZIgSW7Q4aMyXcfqmnv+eB2FfAAoqd77zqkMlLSp1ha+T/EOI+ZOYUzo5+fowdnFk4oEU3zTiYO872BLVMNLuANJ0w6EBDhJBM2+eW8qDGkcKSH4iI7i/gCUV+wmOfT/cuIS6slQoba0HfDLtVntswE/+8bc5UJdKYbKBUhGQ1WMeJ19ViQDEFGnmg+vzkQXeTLmux9Ek2uqXbpUQIDAQAB";

    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://rv8ewqkk.dongtaiyuming.net/payed/notify";
//    public static String notify_url = "http://youmans.free.idcfengye.com/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    public static String sign_type = "RSA2";

    // 字符编码格式
    public static String charset = "utf-8";

    private String timeout="30m";
    // 支付宝网关
    public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    // 支付宝网关
    public static String log_path = "C:\\";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
